﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Dycox.Windows.Forms.Design
{
    public class NumericTextBoxDesigner : ControlDesigner
    {
        public NumericTextBoxDesigner()
        {

        }

        DesignerActionListCollection _actionLists;

        public override System.ComponentModel.Design.DesignerActionListCollection ActionLists
        {
            get
            {
                if (_actionLists == null)
                {
                    _actionLists = new DesignerActionListCollection();
                    _actionLists.Add(new NumericTextBoxActionList(this));
                }
                return _actionLists;
            }
        }

        public override SelectionRules SelectionRules
        {
            get
            {
                return System.Windows.Forms.Design.SelectionRules.LeftSizeable |
                    System.Windows.Forms.Design.SelectionRules.RightSizeable |
                    System.Windows.Forms.Design.SelectionRules.Moveable;
            }
        }

        public override System.Collections.IList SnapLines
        {
            get
            {
                return base.SnapLines;
            }
        }

        class NumericTextBoxActionList : DesignerActionList
        {
            PropertyDescriptorCollection _properties;

            public NumericTextBoxActionList(NumericTextBoxDesigner designer)
                : base(designer.Component)
            {
                _properties = TypeDescriptor.GetProperties(designer.Component);
            }

            public decimal? MaxValue
            {
                get { return (decimal?)GetComponentValue("MaxValue"); }
                set { SetComponentValue("MaxValue", value); }
            }

            public decimal? MinValue
            {
                get { return (decimal?)GetComponentValue("MinValue"); }
                set { SetComponentValue("MinValue", value); }
            }

            public decimal Increment
            {
                get { return (decimal)GetComponentValue("Increment"); }
                set { SetComponentValue("Increment", value); }
            }

            public bool AllowDecimals
            {
                get { return (bool)GetComponentValue("AllowDecimals"); }
                set { SetComponentValue("AllowDecimals", value); }
            }

            private object GetComponentValue(string propertyName)
            {
                return _properties[propertyName].GetValue(base.Component);
            }

            private void SetComponentValue(string propertyName, object value)
            {
                _properties[propertyName].SetValue(this.Component, value);
            }


            public override DesignerActionItemCollection GetSortedActionItems()
            {
                return new DesignerActionItemCollection
                {
                    new DesignerActionPropertyItem("AllowDecimals", "Allows Decimals"),
                    new DesignerActionPropertyItem("MaxValue", "Maximum"),
                    new DesignerActionPropertyItem("MinValue", "Minimum"),
                    new DesignerActionPropertyItem("Increment", "Increment")
                };
            }


        }
    }
}
